// pages/Login/Login.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userImg:'',
    userName:''
  },
  //用户授权
  getUserInfo: function (e) {
    let self = this;
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    console.log(e.detail.userInfo.avatarUrl)
    console.log(e.detail.userInfo.nickName)
    self.getOpenId()
  },
  //获取用户openid
  getOpenId: function () {
    let self = this
    wx.login({
      timeout: 10000,
      success: (result) => {
        if (result.code) {
          wx.request({
            url: `http://localhost:3000/getUserOpenId`,
            data: { code: result.code },
            success: res => {
              console.log('&&&&&')
              console.log(res)
              wx.reLaunch({
                url: '/pages/index/index',
              })
              // self.data.userInfo.openId = res.data.data.openid
              self.addMemberInfo(self.data.userInfo)
            }
          })
        }
      },
      fail: () => { },
      complete: () => { }
    });
  },
  addMemberInfo(e){
    let self = this;
    let userInfo = app.globalData.userInfo;
    var avatarUrl = userInfo.avatarUrl;
    var nickname = userInfo.nickName;
    console.log(avatarUrl)
    console.log(nickname)
    wx.setStorage({
      key: 'userName',
      data: nickname
    })
    self.setData({
      userImg: avatarUrl
    })
    self.setData({
      userName: nickname
    })
    console.log(self.data.userImg)
    console.log(self.data.userName)
    wx.request({
      url: "http://localhost:3000/addminiUser",
      method: 'POST',
      data: {
        userName: self.data.userName,
        userImg: self.data.userImg
      }
    })
    console.log("$$$$$$$$$$$$")
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = app.globalData.userInfo;

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})